﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using sharpPDF;
using sharpPDF.Enumerators;
using sharpPDF.Fonts;
using sharpPDF.Elements;
using sharpPDF.PDFControls;
using sharpPDF.Tables;


namespace Projekt
{
    /// <summary>
    /// Bon-okkal kapcsolatos muveletek
    /// </summary>
    class grn
    {
        Dalgen conn = Dalgen.Instance;

        /// <summary>
        /// Lekeri egy adott Nir sorait
        /// </summary>
        /// <param name="NirNr">Nir azonositoja</param>
        /// <returns>a termekek listaja amelyek a Nir-en szerepelnek(DataTable)</returns>
        public DataTable getProduceList(String NirNr)
        {
            try
            {
                string sqlQuery = "Select Aruk.Anev as Name ,NirSorok.NMennyiseg as Total from Aruk,NirSorok Where NirSorok.NAruId = Aruk.AruID And Nirsorok.NirNr = '" + NirNr + "'";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw es; }
        }

        /// <summary>
        /// Uj Bon de tranfer(BON) hozzaadasa
        /// </summary>
        /// <param name="grnLines">bon sorai</param>
        /// <param name="nirNr">Nir sorszama(amelyikhez tartozik)</param>
        /// <param name="BonDate">bon datuma</param>
        public void addGrn(DataTable grnLines, String nirNr, string BonDate)
        {
            try
            {
                Produce aru = new Produce();
                Shop uzlet = new Shop();
                DataTable shopName = uzlet.getShopNameList();
                int uzletdb = shopName.Rows.Count;
                int index = -1;
                Console.WriteLine("MOST!!!!!");
                foreach (DataRow rows in shopName.Rows)
                {
                    index++;
                    Console.WriteLine(rows[1]);
                    String sqlQuery = "Insert into Bon(BNirNr,BDatum,BUzletID) values('" + nirNr + "','" + BonDate + "','" + rows[1].ToString() + "')";
                     conn.nonQueryCmd(sqlQuery);
                    foreach (DataRow rows1 in grnLines.Rows)
                    {
                        String db = rows1.ItemArray.GetValue(index).ToString();
                        String nev = rows1.ItemArray.GetValue(uzletdb).ToString();
                        String um = rows1.ItemArray.GetValue(uzletdb + 1).ToString();
                        String elar = rows1.ItemArray.GetValue(uzletdb + 4).ToString();
                        if (db != "0")
                        {
                            int AruID = aru.getID(nev, um);
                            string sqlQueryBonNr = "Select BonNr from Bon where BNirNR = '" + nirNr + "' ANd BUzletId = '" + rows[1].ToString() + "'";
                            int BonNr = (int)conn.ScalarCmd(sqlQueryBonNr);
                            sqlQuery = "insert into BonSorok(BonNr,BAruID,BMennyiseg,BAr) values ('" + BonNr + "','" + AruID + "','" + db + "','" + elar + "')";
                            conn.nonQueryCmd(sqlQuery);
                            //   Console.Write(nev + " " + um + " " + db + " " + elar);
                        }
                    }
                    Console.WriteLine();
              //      writeToPDF(nirNr.ToString());
                }
            }
            catch (Exception es) { throw es; }
        }

        /// <summary>
        /// Lekeri egy bon sorait
        /// </summary>
        /// <param name="bonNr">Bon sorszama</param>
        /// <returns>a bon sorai(datatable)</returns>
        private DataTable getBonlines(String bonNr)
        {
            String sqlQuery = "Select Distinct ANev,BMennyiseg,BAr,Bertek from Aruk,BonSorok where AruID = BonSorok.BAruID and BonSorok.BonNR = '" + bonNr + "'";
            return conn.QueryCmd(sqlQuery);
        }

        private String trunc(String value)
        {
            return Math.Round(Convert.ToDouble(value), 2).ToString();
        }

        /// <summary>
        /// Adott Nirhez tartozok bon-ok kiirasa pdf-be(nyomtatasra)
        /// </summary>
        /// <param name="nirNr">Nir sorszama</param>
        public void writeToPDF(String nirNr)
        {
            try
            {
                string firmName;
                string location;
                string Val;

                string sqlQuery = "select BonNR,BDatum,BUzletID from bon where Bnirnr = '" + nirNr + "'";
                DataTable bon = conn.QueryCmd(sqlQuery);

                sqlQuery = "Select Ertek from bealitasok where nev = 'CegNev'";
                firmName = conn.ScalarCmd(sqlQuery).ToString();
                sqlQuery = "Select Ertek from bealitasok where nev = 'Helyseg'";
                location = conn.ScalarCmd(sqlQuery).ToString();




                foreach (DataRow rows in bon.Rows)
                {
                    string file = rows[0].ToString();
                    sqlQuery = "Select BErtek from BonErtekek where BErtekTipus = 1 and BonNr = '" + rows[0].ToString() + "'";
                    Val = conn.ScalarCmd(sqlQuery).ToString();
                    if (File.Exists(file))
                        File.Delete(file);
                    pdfDocument mypdf = new pdfDocument(file, "ReB");
                    pdfPage myPage = mypdf.addPage(700, 500);
                    myPage.addText(firmName, 30, 680, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText(location, 30, 670, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("BON DE TRANSFER MARFA CATRE MAGAZINUL: " + rows[2].ToString(), 150, 660, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("NUMAR BON: " + rows[0].ToString() + " din " + rows[1].ToString(), 170, 650, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("Receptia = " + nirNr, 30, 640, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.drawLine(0, 630, 500, 630, predefinedLineStyle.csNormal);

                    int[] koordx = new int[5] { 30, 60, 130, 160, 200 };

                    myPage.addText("Nr. crt.", koordx[0], 620, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("Sortiment", koordx[1], 620, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("Cantitate", koordx[2], 620, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("Pret(V)", koordx[3], 620, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText("Valoare(V).", koordx[4], 620, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.drawLine(0, 610, 500, 610, predefinedLineStyle.csNormal);

                    DataTable BonLines = new DataTable();
                    BonLines = getBonlines(rows[0].ToString());
                    int y = 610;
                    int line = 1;

                    foreach (DataRow bonRows in BonLines.Rows)
                    {
                        y -= 10;
                        myPage.addText(line.ToString(), koordx[0], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                        line++;
                        myPage.addText(bonRows[0].ToString(), koordx[1], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                        myPage.addText(bonRows[1].ToString(), koordx[2], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                        myPage.addText(bonRows[2].ToString(), koordx[3], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                        myPage.addText(trunc(bonRows[3].ToString()), koordx[4], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    }
                    y -= 10;
                    myPage.drawLine(0, y, 500, y, predefinedLineStyle.csNormal);
                    y -= 10;
                    myPage.addText("TOTAL VALOARE TRANSFERATE PE BON: ", koordx[0], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    myPage.addText(trunc(Val), koordx[4], y, mypdf.getFontReference(predefinedFont.csHelvetica), 6);
                    y -= 10;
                    myPage.drawLine(0, y, 500, y, predefinedLineStyle.csNormal);
                    mypdf.createPDF(@"c:\pdf\" + file + ".PDF");
                    myPage = null;
                    mypdf = null;
                    System.Diagnostics.Process.Start(@"c:\pdf\" + file + ".PDF");
                    System.Threading.Thread.Sleep(250);
                }
            }
            catch (Exception es) { throw es; }
        }

        /// <summary>
        /// Lekeri egy adott bon datumat
        /// </summary>
        /// <param name="nirNr">Nir sorszama(amelyikhez tartozik)</param>
        /// <returns>A Bon datuma</returns>
        public string getGRNDate(String nirNr)
        {
            String sqlQuery = "select Distinct(BDatum) from Bon where BNirNR = '" + nirNr + "'";
            return conn.ScalarCmd(sqlQuery).ToString();
        }
    }
}
